package com.chinapost.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.chinapost.service.DeptServiceI;
import com.chinapost.service.WdryServiceI;
import com.chinapost.vo.Json;
import com.chinapost.vo.SessionInfo;
import com.chinapost.vo.Wdry;
import com.opensymphony.xwork2.ModelDriven;

@Action(value = "wdryAction", results = { 
		@Result(name = "wdryManager", location = "/jsp/wdry/wdryManager.jsp"),
		@Result(name = "wdryAdd", location = "/jsp/wdry/wdryAdd.jsp"), 
		@Result(name = "wdryImport", location = "/jsp/wdry/wdryImport.jsp"),
		@Result(name = "wdryEdit", location = "/jsp/wdry/wdryEdit.jsp") 
		})
public class WdryAction extends BaseAction implements ModelDriven<Wdry>, ServletRequestAware {

	private Wdry wdry = new Wdry();
	private WdryServiceI wdryService;
	private DeptServiceI deptService;
	private HttpServletRequest request;

	public DeptServiceI getDeptService() {
		return deptService;
	}

	@Autowired
	public void setDeptService(DeptServiceI deptService) {
		this.deptService = deptService;
	}

	public WdryServiceI getWdryService() {
		return wdryService;
	}

	@Autowired
	public void setWdryService(WdryServiceI wdryService) {
		this.wdryService = wdryService;
	}

	@Override
	public Wdry getModel() {
		return wdry;
	}

	public String wdryManager() {
		return "wdryManager";
	}

	public String wdryEdit() {
		return "wdryEdit";
	}

	public String wdryAdd() {
		return "wdryAdd";
	}

	public String wdryImport() {
		return "wdryImport";
	}

	public void add() {
		Json j = new Json();
		try {
			wdryService.add(wdry);
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} catch (Exception e) {
			j.setMsg("添加失败！");
		}
		super.writeJson(j);
	}

	public void addWdrys() {
		Json j = new Json();
		int count = 0;
		try {
			count = wdryService.addWdrys(wdry);
			if (count > 0) {
				j.setSuccess(true);
				j.setMsg("上传成功，新增" + count + "条网点人员记录！");
			} else {
				j.setMsg("上传失败,新增0条记录！");
			}
		} catch (Exception e) {
			j.setMsg("上传失败,新增0条记录！");
		}
		super.writeJson(j);
	}

	public void edit() {
		Json j = new Json();
		try {
			wdryService.edit(wdry);
			j.setSuccess(true);
			j.setMsg("编辑成功!");
		} catch (Exception e) {
			j.setMsg("编辑失败！");
		}
		writeJson(j);
	}

	public void delete() {
		Json j = new Json();
		try {
			wdryService.delete(wdry);
			j.setSuccess(true);
			j.setMsg("删除成功！");
			j.setObj(wdry.getCid());
		} catch (Exception e) {
			j.setMsg("删除失败！");
		}
		super.writeJson(j);
	}

	public void datagrid() {
		if (!StringUtils.isEmpty(wdry.getDeptId())) {
			wdry.setDeptIds(deptService.getChildIds(wdry.getDeptId()));
		} else {
			SessionInfo s = (SessionInfo)request.getSession().getAttribute("sessionInfo");
			wdry.setDeptIds(deptService.getChildIds(s.getDeptid()));
		}

		super.writeJsonDate(wdryService.datagrid(wdry));
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}
}
